/* login.js */
layui.use(['form', 'layer'], function () {
    var form = layui.form;
    var layer = layui.layer;

    /* 表单提交 */
    form.on('submit(login)', function(data){
        var index = layer.load();
        $.ajax({
            type :"post",
            url: "/login",
            data :{
                username: data.field.username,
                password: data.field.password,
                rememberMe: data.field.rememberMe === 'on',
                verifyCode: data.field.verifyCode
            },
            dataType:"json",
            success:function (data) {
                layer.close(index);
                if(data.code==200){
                    window.location.href="/main.jsp";
                }else{
                    $("#verifyCode").attr("src", "/verify_image?"+Math.floor(Math.random() * 100));
                    layer.msg(data.message);
                    console.error(data.message)
                }
            },
            error: function (xhr) {
                layer.close(index);
                $("#verifyCode").attr("src", "/verify_image?"+Math.floor(Math.random() * 100));
                layer.msg(xhr.responseJSON.message);
                console.error(xhr.responseJSON.message)
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
});